package com.teamit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.teamit.entity.Document;
import com.teamit.entity.Employee;
import com.teamit.util.db.DBHelp;
import com.teamit.util.db.Page;
import com.teamit.util.db.RowMapper;

public class DocumentDao {
	
	DBHelp<Document> db = new DBHelp<Document>();

	public void save(Document document){
		String sql = "insert into t_document(id,title,content,createtime,project_id,employee_id) values(?,?,?,?,?,?)";
		db.executeSQL(sql, document.getId(),document.getTitle(),document.getContent(),document.getCreatetime(),document.getProjectid(),document.getEmployeeid());
	}
	
	public List<Document> findAllByProjectId(String projectid){
		String sql = "select id,title,content,createtime,project_id,employee_id from t_document where project_id=?";
		return db.executeQueryToList(sql, new DocumentRowMapper(), projectid);
	}	
	
	public void update(Document document){
		String sql = "update t_document set title=?,content=?,createtime=?,project_id=?,employee_id=? where id=?";
		db.executeSQL(sql, document.getTitle(),document.getContent(),document.getCreatetime(),document.getProjectid(),document.getEmployeeid(),document.getId());
	}

	public Document findById(String id) {
		String sql = "select id,title,content,createtime,project_id,employee_id from t_document where id=?";
		return db.executeQueryToObject(sql, new DocumentRowMapper(), id);
	}
	public void delete(String id) {
		String sql="delete from t_document where id=?";
		db.executeSQL(sql, id);
	}
	public int count() {
		String sql = "select count(*) from t_document";
		return db.executeQuery(sql).intValue();
	}
	public Page<Document> findByPageAndProjectId(int pageNum ,String projectid){
		
		Page<Document> page = new Page<Document>(count(), pageNum);
		StringBuilder sql = new StringBuilder();
		sql.append("select td.id,title,content,te.createtime,project_id,employee_id,username from t_document as td ");
		sql.append("LEFT JOIN t_employee as te ON td.employee_id = te.id where Project_id=? limit ?,?");
		
		List<Document> list = db.executeQueryToList(sql.toString(), new RowMapper<Document>(){

			public Document mapRow(ResultSet rs) throws SQLException {
				Document document = new Document();
				document.setId(rs.getString("id"));
				document.setTitle(rs.getString("title"));
				document.setContent(rs.getString("content"));
				document.setCreatetime(rs.getString("createtime"));
				document.setProjectid(rs.getString("project_id"));
				document.setEmployeeid(rs.getString("employee_id")); 
				
				Employee employee = new Employee();
				employee.setName(rs.getString("username"));
				
				document.setEmployee(employee);
				return document;
			}
			
		}, projectid ,page.getStart(),page.getPageSize());
		page.setList(list);
		return page;
	}
	
	private class DocumentRowMapper implements RowMapper<Document>{
		public Document mapRow(ResultSet rs) throws SQLException {
			Document document = new Document();
			document.setId(rs.getString("id"));
			document.setTitle(rs.getString("title"));
			document.setContent(rs.getString("content"));
			document.setCreatetime(rs.getString("createtime"));
			document.setProjectid(rs.getString("project_id"));
			document.setEmployeeid(rs.getString("employee_id")); 
			return document;
		}
		
	}
	
}
